<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="88px"
    >
      <el-form-item label="布撤防状态" prop="armStatus">
        <el-input
          v-model="queryParams.armStatus"
          placeholder="请输入布撤防状态"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-input
          v-model="queryParams.deviceType"
          placeholder="请输入设备类型"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="卡号" prop="deviceIccid">
        <el-input
          v-model="queryParams.deviceIccid"
          placeholder="请输入卡号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="在线状态" prop="onlineStatus">
        <el-select
          v-model="queryParams.onlineStatus"
          placeholder="请选择在线状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.JHL_ONLINE_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="场所ID" prop="placeId">
        <el-input
          v-model="queryParams.placeId"
          placeholder="请输入场所ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="父设备ID" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父设备ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="协议类型" prop="reportType">
        <el-select
          v-model="queryParams.reportType"
          placeholder="请选择协议类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="公司主域" prop="mainCode">
        <el-input
          v-model="queryParams.mainCode"
          placeholder="请输入公司主域"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="离线时间" prop="lastOfflineTime">
        <el-date-picker
          v-model="queryParams.lastOfflineTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备序列号" prop="deviceIdentifier">
        <el-input
          v-model="queryParams.deviceIdentifier"
          placeholder="请输入设备序列号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="MAC" prop="mac">
        <el-input
          v-model="queryParams.mac"
          placeholder="请输入MAC"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公司子域" prop="subCode">
        <el-input
          v-model="queryParams.subCode"
          placeholder="请输入公司子域"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="上线时间" prop="lastOnlineTime">
        <el-date-picker
          v-model="queryParams.lastOnlineTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['iot:jhl-equipment-device:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['iot:jhl-equipment-device:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="primary" plain @click="asyncJhlEquipmentDevice">
          <Icon icon="ep:refresh" class="mr-5px" /> 同步
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="设备ID" align="center" prop="id" />
      <el-table-column label="布撤防状态" align="center" prop="armStatus" />
      <el-table-column label="设备类型" align="center" prop="deviceType" />
      <el-table-column label="卡号" align="center" prop="deviceIccid" />
      <el-table-column label="在线状态" align="center" prop="onlineStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.JHL_ONLINE_STATUS" :value="scope.row.onlineStatus" />
        </template>
      </el-table-column>
      <el-table-column label="场所ID" align="center" prop="placeId" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="父设备ID" align="center" prop="parentId" />
      <el-table-column label="协议类型" align="center" prop="reportType" />
      <el-table-column label="公司主域" align="center" prop="mainCode" />
      <el-table-column
        label="离线时间"
        align="center"
        prop="lastOfflineTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="设备序列号" align="center" prop="deviceIdentifier" />
      <el-table-column label="MAC" align="center" prop="mac" />
      <el-table-column label="公司子域" align="center" prop="subCode" />
      <el-table-column
        label="上线时间"
        align="center"
        prop="lastOnlineTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            v-if="false"
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['iot:jhl-equipment-device:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['iot:jhl-equipment-device:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <JhlEquipmentDeviceForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { JhlEquipmentDeviceApi, JhlEquipmentDeviceVO } from '@/api/iot/jhlequipmentdevice'
import JhlEquipmentDeviceForm from './JhlEquipmentDeviceForm.vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

/** 精华隆设备信息 列表 */
defineOptions({ name: 'JhlEquipmentDevice' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<JhlEquipmentDeviceVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  armStatus: undefined,
  deviceType: undefined,
  deviceIccid: undefined,
  onlineStatus: undefined,
  placeId: undefined,
  deviceName: undefined,
  parentId: undefined,
  reportType: undefined,
  mainCode: undefined,
  lastOfflineTime: [],
  deviceIdentifier: undefined,
  mac: undefined,
  subCode: undefined,
  lastOnlineTime: [],
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await JhlEquipmentDeviceApi.getJhlEquipmentDevicePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 同步按钮操作 */
const asyncJhlEquipmentDevice = async () => {
  try {
    // 发起同步
    await JhlEquipmentDeviceApi.asyncJhlEquipmentDevice()
    message.success(t('common.success'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await JhlEquipmentDeviceApi.deleteJhlEquipmentDevice(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await JhlEquipmentDeviceApi.exportJhlEquipmentDevice(queryParams)
    download.excel(data, '精华隆设备信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
